package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"math"
	"xinhua_bookstore_db/cache"
	"xinhua_bookstore_db/models"
)

type CollectController struct {
	beego.Controller
}

// pageMsg
type PageMsg struct {
	Code    int         `json:"code"`
	Msg     string      `json:"msg"`
	Data    interface{} `json:"data"`
	Page    int         `json:"page"`
	Size    int         `json:"size"`
	Count   int64       `json:"count"`
	Sumpage int         `json:"sumpage"`
}

func (c *CollectController) List() {
	//跨域
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")

	//分页
	page, _ := c.GetInt("page", 1)     //默认显示1页
	pageSize, _ := c.GetInt("size", 5) //默认显示5页

	//接收总条数
	var count int64
	models.DB.Model(&models.Books{}).Count(&count)
	//计算总页数
	sumPage := int(math.Ceil(float64(count) / float64(pageSize)))
	if page > sumPage {
		page = sumPage
	}
	//计算偏移量
	offset := (page - 1) * pageSize

	//接收用户id
	userId, _ := c.GetInt("user_id")
	//定义书籍id
	var booksId []interface{}
	//实现关联查询
	models.DB.Model(&models.Collect{}).Select("books_id").Where("user_id = ?", userId).Find(&booksId)

	//接收书籍结构体
	var books []models.Books
	//缓存redis数据
	cacheData := cache.Rdb.Get(cache.Ctx, "collect_list").Val()
	if cacheData == "" {
		//从redis中获取数据  查询用户id收藏的小说
		models.DB.Where("id in (?)", booksId).Offset(offset).Limit(pageSize).Find(&books)
		jsonData, _ := json.Marshal(books)
		cache.Rdb.Set(cache.Ctx, "collect_list", jsonData, 0)
	} else {
		//从mysql中获取数据
		json.Unmarshal([]byte(cacheData), &books)
	}

	res := PageMsg{
		Code:    200,
		Msg:     "查询成功",
		Data:    books,
		Page:    page,
		Size:    pageSize,
		Count:   count,
		Sumpage: sumPage,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return
}

//缓存列表
func (c *CollectController) List1() {
	//跨域
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")

	//接收书籍结构体
	var books []models.Books
	//缓存redis数据
	cacheData := cache.Rdb.Get(cache.Ctx, "collect_list").Val()
	if cacheData == "" {
		//从redis中获取数据  查询用户id收藏的小说
		//接收用户id
		userId, _ := c.GetInt("user_id")
		//定义书籍id
		var booksId []interface{}
		//实现关联查询
		models.DB.Model(&models.Collect{}).Select("books_id").Where("user_id = ?", userId).Find(&booksId)

		models.DB.Where("id in (?)", booksId).Find(&books)
		jsonData, _ := json.Marshal(books)
		cache.Rdb.Set(cache.Ctx, "collect_list", jsonData, 0)
	} else {
		//从mysql中获取数据
		json.Unmarshal([]byte(cacheData), &books)
	}

	res := Response{
		Code: 200,
		Msg:  "查询成功",
		Data: books,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 记录删除
func (c *CollectController) Del() {
	//接收id
	id, _ := c.GetInt("id")
	//接收结构体
	var collect models.Collect
	models.DB.Where("id=?", id).First(&collect)
	//判断状态
	if collect.Status == "已完成" {
		res := Response{
			Code: 500,
			Msg:  "已完成交易的记录禁止删除",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	//实例化id
	collect.ID = uint(id)
	if collect.Del(id) != nil {
		res := Response{
			Code: 500,
			Msg:  "删除失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res := Response{
		Code: 200,
		Msg:  "删除成功",
		Data: nil,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}
